package com.example.myblog.controller;

import com.example.myblog.domain.Blog;
import com.example.myblog.domain.User;
import com.example.myblog.service.blog.BlogService;
import com.example.myblog.service.user.UserServiceImpl;
import com.example.myblog.util.COOKIEUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description: 交互类
 * @Date: Created in 16:01 2020/11/10
 * @Modefied By:
 */
@RestController
public class BlogController {

    @Resource
    HttpServletRequest request;
    @Resource
    HttpServletResponse response;


    @Resource
    private BlogService blogService;

    @Resource
    private UserServiceImpl userService;

    Map map = new HashMap();

    /**
     *  cookie状态
     */
    private int getCookie(){
        int i = 0;

        Cookie cookie = COOKIEUtil.getCookieByName(request,"bUsername");
        if(cookie!=null){
            String uuid = cookie.getValue();

            if(uuid==null){
                map.put("code", 13);
                map.put("msg", "请登录");
            }else{
                List<User> list = userService.queryForUuid(uuid);
                System.out.println("list:"+list);

                if(list.size()!=0){
                    int uid = list.get(0).getId();


                    if(uid>0) {
                        i = uid;

                    }else{
                        map.put("code", 13);
                        map.put("msg", "用户不存在");
                    }
                }else{
                    map.put("code", 13);
                    map.put("msg", "登录过期");
                }

            }
        }else{
            map.put("code", 13);
            map.put("msg", "请登录");
        }





        return i;
    }


    /**
    * @desc: 添加博客
    */
    @RequestMapping(value = "/addBlog",method = RequestMethod.POST)
    public Object addBlog(@RequestBody Blog blog){
        int uid = this.getCookie();
        Blog blog1 = blogService.save(blog);
        if(uid>0){//uid存在
            if(blog1!=null){
                map.put("code", 200);
                map.put("msg", "添加成功");
            }else{
                map.put("code", -1);
                map.put("msg", "添加失败");
            }

        }

        return map;
    }

    /**
    * @desc: 删除
    */
    @RequestMapping("/delBlog")
    @ResponseBody
    public Object delBlog(Integer id){
        int uid = this.getCookie();//
        if(uid>0){
            boolean result = blogService.deleteById(id);
            String str = "删除失败";
            int code = -1;
            if(result){
                str = "删除成功";
                code = 200;
            }

            map.put("msg",str);
            map.put("code",code);
        }


        return map;
    }


    /**
    * @desc: 修改  传id
    */
    @RequestMapping(value = "/editBlog",method = RequestMethod.POST)
    public Object editBlog(@RequestBody Blog blog){
        int uid = this.getCookie();
        if(uid>0){
            Blog b = blogService.edit(blog);
            if(b!=null){
                map.put("msg","修改成功");
                map.put("code",-1);
            }else{
                map.put("msg","修改失败");
                map.put("code",200);
            }
        }


        return map;
    }



    /**
    * @desc: 根据关键字查询
    */
    @GetMapping("/getBlogByKeyword")
    public Object getBlogByKeyword(String keyword){
        int uid = this.getCookie();

        if(uid>0){
            List blog = blogService.findByTitleLike("%"+keyword+"%",uid);
            if(blog!=null){
                map.put("list",blog);
                map.put("code",200);
                map.put("msg","查询成功");
            }else{
                map.put("code",-1);
                map.put("msg","查询失败");
            }

        }

        return map;
    }



    /**
    * @desc: 查询全部
    */
    @RequestMapping("/getBlogAll")
    public Object selectBlogAll(){
        List<Blog> blog = blogService.findAll();
        map.put("code",200);
        map.put("msg","查询成功");
//        System.out.println(blog);
        map.put("list",blog);
        return map;
    }



    /**
    * @desc: 根据id查询
    */
    @RequestMapping("/getBlogById")
    public Map findUserById(Integer id){
        int uid = this.getCookie();
        if(uid>0){

            Blog blog = blogService.findById(id);
            if(blog!=null){
                map.put("code",200);
                map.put("msg","查询成功");
                map.put("list",blog);
            }else{
                map.put("code",-1);
                map.put("msg","查询失败");
            }

        }

        return map;
    }





    /**
     * @desc: 根据uid查询  查询单个用户的
     */
    @RequestMapping("/getBlogByUid")
    public Map findUserByUid(Integer uid){


        int i = this.getCookie();

        if(i>0){

            if(uid==null){
                map.put("code",-1);
                map.put("msg","用户id为必填");
            }else{
                List<Blog> userlist = blogService.findAllByUid(uid);
                if(userlist!=null){
                    map.put("code",200);
                    map.put("msg","获取成功");
                    map.put("list",userlist);
                }else{
                    map.put("code",-1);
                    map.put("msg","用户id不存在");
                }
            }

        }


        return map;
    }

}
